import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common'
import { UserService } from '../services/user.service'
import { CreateUserDto } from '../dto/create-user.dto'
import { UpdateUserDto } from '../dto/update-user.dto'
import { PermissionsGuard } from 'src/modules/rbac/guards/permissions.guard'
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery, ApiBody } from '@nestjs/swagger'
import { User } from '../entities/user.entity'
import { Permissions } from 'src/common/decorators/permissions.decorator'

/**
 * 用户控制器
 * 提供用户管理的RESTful API接口
 */
@ApiTags('用户管理')
@Controller('users')
@UseGuards(PermissionsGuard)
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * 创建用户
   */
  @Post()
  @ApiOperation({ summary: '创建用户' })
  @ApiBody({ type: CreateUserDto })
  @ApiResponse({ status: 201, description: '用户创建成功', type: User })
  @ApiResponse({ status: 400, description: '用户名或邮箱已存在' })
  @Permissions('user:create')
  async create(@Body() createUserDto: CreateUserDto): Promise<User> {
    return this.userService.create(createUserDto)
  }

  /**
   * 获取用户列表
   */
  @Get()
  @ApiOperation({ summary: '获取用户列表' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({
    name: 'limit',
    required: false,
    description: '每页数量',
    example: 10,
  })
  @ApiResponse({ status: 200, description: '用户列表', type: [User] })
  @Permissions('user:list')
  async findAll(
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 10,
  ): Promise<{ data: User[]; total: number }> {
    return this.userService.findAll(page, limit)
  }

  /**
   * 根据ID获取用户详情
   */
  @Get(':id')
  @ApiOperation({ summary: '获取用户详情' })
  @ApiParam({ name: 'id', description: '用户ID' })
  @ApiResponse({ status: 200, description: '用户详情', type: User })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @Permissions('user:read')
  async findOne(@Param('id') id: number): Promise<User> {
    return this.userService.findOne(id)
  }

  /**
   * 更新用户信息
   */
  @Patch(':id')
  @ApiOperation({ summary: '更新用户信息' })
  @ApiParam({ name: 'id', description: '用户ID' })
  @ApiBody({ type: UpdateUserDto })
  @ApiResponse({ status: 200, description: '更新成功', type: User })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @Permissions('user:update')
  async update(@Param('id') id: number, @Body() updateUserDto: UpdateUserDto): Promise<User> {
    return this.userService.update(id, updateUserDto)
  }

  /**
   * 删除用户
   */
  @Delete(':id')
  @ApiOperation({ summary: '删除用户' })
  @ApiParam({ name: 'id', description: '用户ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @HttpCode(HttpStatus.OK)
  @Permissions('user:delete')
  async remove(@Param('id') id: number): Promise<void> {
    return this.userService.remove(id)
  }

  /**
   * 更新用户状态
   */
  @Patch(':id/status')
  @ApiOperation({ summary: '更新用户状态' })
  @ApiParam({ name: 'id', description: '用户ID' })
  @ApiBody({ schema: { example: { status: 1 } } })
  @ApiResponse({ status: 200, description: '状态更新成功', type: User })
  @Permissions('user:update-status')
  async updateStatus(@Param('id') id: number, @Body('status') status: number): Promise<User> {
    return this.userService.updateStatus(id, status)
  }

  /**
   * 根据用户名查找用户
   */
  @Get('search/username/:username')
  @ApiOperation({ summary: '根据用户名查找用户' })
  @ApiParam({ name: 'username', description: '用户名' })
  @ApiResponse({ status: 200, description: '用户信息', type: User })
  @Permissions('user:search')
  async findByUsername(@Param('username') username: string): Promise<User | null> {
    return this.userService.findByUsername(username)
  }

  /**
   * 根据邮箱查找用户
   */
  @Get('search/email/:email')
  @ApiOperation({ summary: '根据邮箱查找用户' })
  @ApiParam({ name: 'email', description: '邮箱' })
  @ApiResponse({ status: 200, description: '用户信息', type: User })
  @Permissions('user:search')
  async findByEmail(@Param('email') email: string): Promise<User | null> {
    return this.userService.findByEmail(email)
  }
}
